﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using VH.SharePoint.Api.Entensions;

namespace VH.SharePoint.Api.WLW
{
    public class WlwManifestHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if(context.Request.HttpMethod != "GET")
                throw new NotSupportedException();

            SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using(var site = new SPSite(new Uri(context.Request.Url,context.Request.RawUrl).ToString()))
                    using (var web = site.OpenWeb())
                    {
                        var blog = BlogWeb.GetBlogWeb(web);
                        uint lang = web.Language;
                        var response = context.Response;
                        //$Resources:wss,settings_pagetitle
                        var manifest = new Manifest();
                        manifest.Options = Supports.Default(ClientType.SharePoint);

                        manifest.Options.Scripts = blog.ScriptTagsEnabled.ToSupportsFeature();
                        manifest.Options.Embeds = manifest.Options.Scripts;

                        //Unsupported yet
                        manifest.Options.Categories = SupportsFeature.No;
                        manifest.Options.CategoriesInline = SupportsFeature.No;
                        manifest.Options.MultipleCategories = SupportsFeature.No;
                        
                        manifest.Buttons = new List<Button>
                            {
                                new Button{ Id = 0, Text = SPUtility.GetLocalizedString("$Resources:Title_Settings_Page", "VH.SharePoint.Api.Blog", lang), ClickUrl = "<![CDATA[\r\n" + web.Url+SPConstants.BlogApiSettingsPage+"\r\n]]>"},
                                new Button{ Id = 1, Text = SPUtility.GetLocalizedString("$Resources:nav_Settings", "core", lang), ClickUrl = "<![CDATA[\r\n" + web.Url+"/_layouts/settings.aspx\r\n]]>"}
                            };
                        
                        manifest.WebLog = new WebLog
                        {
                            ServiceName = web.Title,
                            HomePageLinkText = web.Title,
                            AdminLinkText = SPUtility.GetLocalizedString("$Resources:Title_Settings_Page", "VH.SharePoint.Api.Blog", lang),
                            AdminUrl = site.MakeFullUrl(web.ServerRelativeUrl) + SPConstants.BlogApiSettingsPage,
                            ImageUrl = string.IsNullOrEmpty(web.SiteLogoUrl)?string.Empty:web.Site.MakeFullUrl(web.SiteLogoUrl),
                            PostEditingUrl = "<![CDATA[\r\n" + site.MakeFullUrl(web.ServerRelativeUrl) + "/_layouts/listform.aspx?PageType=6&ListId=" + blog.PostLibraryId + "&ID={post-id}\r\n]]>"


                        };
                        manifest.Views = new Views();
                        var xml = manifest.ToString();
                        response.ContentType = "application/xml";
                        response.Write(xml);
                    }
                });

            
        }

        public bool IsReusable {
            get { return false; }
        }
    }
}
